package com.qmds.deviceserver.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.qmds.common.core.entity.Product;
import com.qmds.common.core.utils.CodeGeneratorUtils;
import com.qmds.common.core.vo.R;
import com.qmds.deviceserver.device.param.DeviceEditParam;
import com.qmds.deviceserver.device.service.DeviceService;
import com.qmds.deviceserver.product.dao.ProductMapper;
import com.qmds.deviceserver.product.param.EditStatusParam;
import com.qmds.deviceserver.product.param.ProductImageParam;
import com.qmds.deviceserver.product.param.ProductParam;
import com.qmds.deviceserver.product.service.ProductService;
import com.qmds.deviceserver.product.vo.ProductVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * --- 代码敲烂 月薪过万 ---
 *
 * @author ShiJiaWei
 * @date 2024/9/11
 * @desc:
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Resource
    private ProductMapper productMapper;

    @Resource
    private DeviceService deviceService;

    @Override
    public R<ProductVO> getProductById(Integer did) {
        ProductVO productVO = productMapper.getProductById(did);
        if (productVO != null) {
            return R.ok(productVO);
        }
        return R.ok("该设备没有连接产品，请先新增产品！", null);
    }

    @Override
    public R<Integer> updateStatus(EditStatusParam statusParam) {
        if (statusParam == null) {
            return R.fail("参数为空");
        }
        DeviceEditParam param = new DeviceEditParam();
        param.setId(statusParam.getDid());
        param.setStatus(statusParam.getStatus());

        return deviceService.updateDevice(param);
    }

    @Override
    public R<Integer> updateProductImage(ProductImageParam imageParam) {
        if (imageParam == null) {
            return R.fail("参数为空");
        }
        int updated = productMapper.update(new LambdaUpdateWrapper<Product>()
                .set(Product::getImageUrl, imageParam.getImageUrl())
                .eq(Product::getId, imageParam.getId()));
        if (updated > 0) {
            return R.ok("修改成功", updated);
        }
        return R.fail();
    }

    @Transactional
    @Override
    public R<Integer> updateProduct(ProductParam param) {
        if (param == null) {
            return R.fail("参数为空");
        }
        Product product = new Product();
        BeanUtils.copyProperties(param, product);
        if (productMapper.updateById(product) > 0) {
            if (param.getStatus() != null) {
                DeviceEditParam editParam = new DeviceEditParam();
                editParam.setId(param.getId());
                editParam.setStatus(param.getStatus());
                deviceService.updateDevice(editParam);
            }
            return R.ok("修改成功", null );
        }
        return R.fail();
    }

    @Override
    public R<Integer> addProduct(ProductParam param) {
        if (param == null) {
            return R.fail("参数有误");
        }
        Product product = new Product();
        BeanUtils.copyProperties(param, product);
        product.setDeviceModel(CodeGeneratorUtils.generateCode(2, 1, false));
        if (productMapper.insert(product) > 0) {
            if (param.getStatus() != null) {
                DeviceEditParam editParam = new DeviceEditParam();
                editParam.setId(param.getDid());
                editParam.setStatus(param.getStatus());
                deviceService.updateDevice(editParam);
            }
            return R.ok("添加成功", null);
        }
        return R.fail();
    }
}
